package com.stats.ultimate;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.LayoutParams;

public class Choose extends Activity {
	// Declare variables to hold a database helper and SQLiteDatabse variable
	SQLiteDatabase db;
	DatabaseHelper db_helper;
	
	// Declare variable to hold integer argument
	int arg;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose);
        
        // Check if savedInstanceState is non-null
        if(savedInstanceState != null){
        	// If so then get arg from previous version of this activity
        	arg = savedInstanceState.getInt("arg");
        }
        
        // Else this is the first instance of this activity
        else{
        	// If so then get arg from previous activity
        	arg = this.getIntent().getIntExtra("arg", 0);
        }
        
        // Initialize linearLayout defined in the layout file
        LinearLayout l2 = (LinearLayout) findViewById(R.id.linearLayout1);
        
        // Set the orientation of this layout to vertical
        l2.setOrientation(LinearLayout.VERTICAL);
        
        // Initialize a new database helper variable
        db_helper = new DatabaseHelper(this);
        
        // Open a database for reading and writing
        db = db_helper.getWritableDatabase();
        
        // Get the number of teams in the database
        int num_teams = db_helper.getNumberTeams(db);
        
        // Close the database
        db.close();
        
        // Create an array to hold buttons
        ArrayList<Button> buttons = new ArrayList<Button>();
        
        // Iterate through each team
        for(int i = 1; i <= num_teams; i++){
        	// Save this teams id as a final variable
        	final int id = i;
        	
        	// Open the database for reading
        	db = db_helper.getWritableDatabase();
        	
        	// Get this teams name and save it
        	String name = db_helper.getTeamName(db, id);
        	
        	// Get this teams number of players and save it
        	final int num_players = db_helper.getNumberPlayers(db, id);
        	
        	// Close the database
        	db.close();
        	
        	// Create a new button
        	Button temp = new Button(this);
        	
        	// Set the text of this button to be the team name
        	temp.setText(name);
        	
        	// Define the layout parameters to be used for buttons
            LinearLayout.LayoutParams temp_params = new LinearLayout.LayoutParams(
            		LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
            	
            // Set the gravity to center
            temp_params.gravity = android.view.Gravity.CENTER;
                        
            // Set all the left and right and bottom margins of this button to be 10
            temp_params.setMargins(10, 0, 10, 10);
            
            // Set the parameters of this button
            temp.setLayoutParams(temp_params);
            
            // Add this button to the layout inside the ScrollView
            l2.addView(temp);
            
            // Set onClickListener for this button
            temp.setOnClickListener(new View.OnClickListener() {
				// Override onClick function
				@Override
				public void onClick(View v) {
					// Check if arg is 0
					if(arg == 0){
						// If so initialize intent for Edit activity
						Intent start_edit = new Intent(getApplicationContext(), Edit.class);
					
						// Put id of this team into bundle for start_edit
						start_edit.putExtra("id", id);
					
						// Start edit activity
						startActivity(start_edit);
					}
					
					// Else check if arg is 1
					else if(arg == 1){
						// If so check if num_players is greater than 0
						if(num_players > 0){
							// If so initialize intent for Game activity
							Intent start_game = new Intent(getApplicationContext(), Game.class);
					
							// Put id of this team into bundle for start_game
							start_game.putExtra("id", id);
					
							// Start game activity
							startActivity(start_game);
						}
						
						// Else give an error message
						else{
							Toast.makeText(getApplicationContext(), "Cannot start a game with an emtpy team.", Toast.LENGTH_SHORT).show();
						}
					}
					
					// Else arg is 2
					else{
						// If so check if num_players is greater than 0
						if(num_players > 0){
							// If so initialize intent for Stats activity
							Intent start_stats = new Intent(getApplicationContext(), Stats.class);
					
							// Put id of this team into bundle for start_stats
							start_stats.putExtra("id", id);
					
							// Start stats activity
							startActivity(start_stats);
						}
						
						// Else this team is empty
						else{
							// If so give an error message
							Toast.makeText(getApplicationContext(), "Cannot view stats of an emtpy team.", Toast.LENGTH_SHORT).show();
						}
					}
				}
			});
        }
    }
    
    // Override onSavedInstanceState function
    @Override
    public void onSaveInstanceState(Bundle outState){
    	// Call super function
    	super.onSaveInstanceState(outState);
    	
    	// Put argument from this activity into outState bundle
    	outState.putInt("arg", arg);
    }
}
